package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {

	s := "ok哈哈!" // UTF-8
	fmt.Println(s)

	for _, data := range []byte(s) {
		fmt.Printf("%X ", data) // 6F 6B E5 93 88 E5 93 88 21
	}

	fmt.Println()

	for i, ch := range s { // ch is rune
		fmt.Printf("(%d, %X) ", i, ch) // (0, 6F) (1, 6B) (2, 54C8) (5, 54C8) (8, 21)
	}

	fmt.Println()

	fmt.Println("Char count: ", len(s))                   // 9
	fmt.Println("Rune count:", utf8.RuneCountInString(s)) // 5

	bytes := []byte(s)
	for len(bytes) > 0 {
		ch, size := utf8.DecodeRune(bytes)
		bytes = bytes[size:]
		fmt.Printf("%c ", ch) // o k 哈 哈 !
	}

	fmt.Println()

	for i, ch := range []rune(s) {
		fmt.Printf("(%d, %c) ", i, ch) // (0, o) (1, k) (2, 哈) (3, 哈) (4, !)
	}

}
